package com.asura.framework.inst;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.asura.framework.rulemodel.RuleInstOutModel;
import com.asura.framework.rulemodel.RuleModelHelper;
import com.asura.framework.rulemodel.TreeNode;
import com.asura.framework.rulemodel.engine.RuleTreeEngine;
import com.asura.framework.rulemodel.engine.dao.entity.RuleInstEntity;
import com.asura.framework.rulemodel.engine.dao.write.RuleInstWriteMapper;
import com.asura.framework.rulemodel.exception.IllegalRuleNameException;

/**
 * 默认规则定义引擎实现.
 * 
 * @author a123
 *
 */
public class RuleInstEngineSupport implements RuleInstEngine
{

	private RuleInstWriteMapper ruleInstWriteMapper;

	public void setRuleInstWriteMapper( RuleInstWriteMapper ruleInstWriteMapper )
	{
		this.ruleInstWriteMapper = ruleInstWriteMapper;
	}

	private RuleTreeEngine ruleTreeEngine;

	public void setRuleTreeEngine( RuleTreeEngine ruleTreeEngine )
	{
		this.ruleTreeEngine = ruleTreeEngine;
	}

	private RuleInstConverter ruleInstConverter = new RuleInstConverter.DefaultRuleInstConverter( );

	public void setRuleInstConverter( RuleInstConverter ruleInstConverter )
	{
		this.ruleInstConverter = ruleInstConverter;
	}

	@Override
	public RuleInstOutModel getRuleInstByInstance( String ruleName, String bizId )
	{
		boolean unlawful = RuleModelHelper.unlawfulRuleName( ruleName );
		if ( unlawful )
		{
			throw new IllegalRuleNameException( "规则名称[" + ruleName + "], 不符合规范. 必须以'_rule_'开始." );
		}
		if ( bizId == null || bizId.trim( ).isEmpty( ) )
		{
			return null;
		}

		TreeNode treeNode = ruleTreeEngine.getTreeNode( ruleName );
		if ( treeNode == null )
		{
			throw new IllegalRuleNameException( "规则名称[" + ruleName + "]不存在." );
		}

		RuleInstEntity ruleInst = ruleInstWriteMapper.queryRuleInstByName( treeNode.getName( ), bizId );
		RuleInstOutModel outModel = ruleInstConverter.convert( ruleInst, treeNode );
		return outModel;
	}

	@Override
	public List< RuleInstOutModel > getRuleInstByGroup( String group, String bizId )
	{
		boolean unlawful = RuleModelHelper.unlawfulRuleGroup( group );
		if ( unlawful )
		{
			throw new IllegalRuleNameException( "规则组[" + group + "], 不符合规范. 名称必须以'_group_'开始." );
		}

		List< TreeNode > treeNodes = ruleTreeEngine.getTreeNodesByGroup( group );
		if ( treeNodes.isEmpty( ) )
		{
			throw new IllegalRuleNameException( "规则组错误, 请检查组 [" + group + "] 是否存在." );
		}

		String[ ] ruleNames = RuleModelHelper.assemblyRuleNames( treeNodes );
		List< RuleInstEntity > ruleInsts = ruleInstWriteMapper.queryRuleInstByGroup( ruleNames, bizId );

		List< RuleInstOutModel > outModels = ruleInstConverter.convert( ruleInsts, treeNodes );
		return outModels;
	}

	@Override
	public Map< String, List< RuleInstOutModel > > getRuleInstByGroup( String[ ] groups, String bizId )
	{
		Map< String, List< RuleInstOutModel > > rules = new HashMap< String, List< RuleInstOutModel > >( );

		for ( String group : groups )
		{
			List< RuleInstOutModel > ruleInsts = getRuleInstByGroup( group, bizId );
			rules.put( group, ruleInsts );
		}

		return rules;
	}
}
